#include "core_client.h"

CoreClient::CoreClient(int cellid,int socketfd):sendbuff(SEND_BUFF_SIZE),recvbuff(RECV_BUFF_SIZE)
{
	this->cellid = cellid;
	this->socketfd = socketfd;
}

CoreClient::~CoreClient()
{
	
}

int CoreClient::getsockfd()
{
	return this->socketfd;
}

int CoreClient::dataReceived()
{
	int ret = 0;
	int last = recvbuff.getLastSize();
	if (last > 0)
	{
		int nlen = recv(this->socketfd, recvbuff.getBuff() + recvbuff.getPos(), last, 0);
		if (nlen > 0)
		{
			recvbuff.addPos(nlen);
		}
		ret = nlen;
	}
	return ret;
}

int CoreClient::parselen()
{
	int datalen = 0;
	int curpos = recvbuff.getPos();
	if (curpos > PACKET_HEAD_LEN)
	{
		CoreStream* headstream = new CoreStream(recvbuff.getBuff(), curpos);
		datalen = headstream->readUInt16();
		delete headstream;
		if ((curpos - 2) < datalen)
		{
			datalen = 0;
		}
		else
		{
			datalen += 2;
		}
	}
	return datalen;
}

char* CoreClient::getMessage()
{
	return recvbuff.getBuff();
}

void CoreClient::delMessage(int len)
{
	recvbuff.pop(len);
}
